--[[
	桌子的共用功能
--]]

local DeskBase = class("DeskBase")

function DeskBase:ctor()
	self.timer = nil --定时器
	self.desk_id = nil
end


--给指定玩家发消息
function DeskBase:sendToUser(user_id,data)
	--判断玩家是否在线
	for k, v in pairs(data) do 
		self.node_message:sendNodeProxy(self.node_cluster_id, 'send_to_user', user_id, k, v)
	end
end

--
--给桌子所有玩家广播消息
function DeskBase:sendToAllUser(data)
	local user_list = g.user_manager:getUserList()
	local users = {}
	for user_id,v in pairs(user_list) do 
		--判断是否在线
		table.insert(users,user_id)		
	end
	if next(users) then
		--让nodeserver转发
		for k, v in pairs(data) do 
			self.node_message:sendNodeProxy(self.node_cluster_id, 'broadcast_to_user', users, k, v)		
		end
	end
end

--给别的玩家发消息
function DeskBase:sendToOther(my_id,data)
	local user_list = g.user_manager:getUserList()
	local users = {}
	for user_id,v in pairs(user_list) do 
		--判断是否在线
		if user_id~=my_id then 
			table.insert(users,user_id)		
		end
	end
	if next(users) then
		--让nodeserver转发
		for k, v in pairs(data) do 
			self.node_message:sendNodeProxy(self.node_cluster_id, 'broadcast_to_user', users, k, v)		
		end
	end
end

--给无座玩家发消息
function DeskBase:sendToNoSeat(data)
	local user_list = g.user_manager:getUserList()
	local users = {}
	for user_id,v in pairs(user_list) do 
		--判断是否在线
		--判断是否有座位
		-- if user_id~=my_id then 
			table.insert(users,user_id)		
		-- end
	end
	if next(users) then
		--让nodeserver转发
		for k, v in pairs(data) do 
			self.node_message:sendNodeProxy(self.node_cluster_id, 'broadcast_to_user', users, k, v)		
		end
	end
end

--清除离线的玩家
function DeskBase:removeDisconnect()
	local user_list = g.user_manager:getUserList()	
	for user_id, user in pairs(user_list) do 
		-- print("----------checkUser ", v.chip)
		if user:getDisconnect() then
			g.user_manager:removeUser(user_id)
			-- self:updatePlayerNum()
			print("踢走掉线#######",user_id)
		end
	end
end

--当前状态
function DeskBase:getState()
	return self.fsm:get()
end

--状态对应的数字
function DeskBase:getStateNum()
	local state = self:getState()
	for k, v in pairs(self.stateTable) do 
		if state == v[1] then 
			return k
		end
	end
	return 0
end



return DeskBase